package com.qf.st.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.st.entity.ResultData;
import com.qf.st.entity.Student;
import com.qf.st.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * StudentController
 *
 * @author
 * @date 2024-05-31 9:48
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/getByName")
    public ResultData getByName(String name) {
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.like("name", name);
        List<Student> list = studentService.list(qw);
        return new ResultData(true,list);
    }

    @GetMapping("/all")
    public ResultData all() {
        List<Student> list = studentService.list();
        return new ResultData(true,list);
    }

    @PostMapping("/del")
    public ResultData del(@RequestBody Student student) {
        boolean b = studentService.removeById(student.getId());
        if (b) {
            return new ResultData(true,"删除成功");
        }
        return new ResultData(false,"删除失败");
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody Student student) {
        boolean b = studentService.updateById(student);
        if (b) {
            return new ResultData(true,"更新成功");
        }
        return new ResultData(false,"更新失败");
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody Student student) {
        boolean b = studentService.save(student);
        if (b) {
            return new ResultData(true,"保存成功");
        }
        return new ResultData(false,"保存失败");
    }
}
